package zmm_shop

import (
	"context"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/errors/gerror"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/os/glog"
	"github.com/tiger1103/gfast/v3/api/v1/zmm"
	"github.com/tiger1103/gfast/v3/internal/app/zmm/dao"
	"github.com/tiger1103/gfast/v3/internal/app/zmm/model/do"
	"github.com/tiger1103/gfast/v3/internal/app/zmm/model/entity"
	"github.com/tiger1103/gfast/v3/internal/app/zmm/service"
	"github.com/tiger1103/gfast/v3/library/liberr"
)

func init() {
	service.RegisterZmmEmployee(New())
}

func New() *sZmmEmployee {
	return &sZmmEmployee{}
}

type sZmmEmployee struct {
}
type OZmmEmployee struct{}

func (s *sZmmEmployee) GetList(ctx context.Context, req *zmm.ZmmEmployeeListReq) (total interface{}, list []*entity.ZmmEmployee, err error) {
	where := g.Map{}
	if req.Name != "" {
		where["name like ?"] = "%" + req.Name + "%"
	}
	if req.Status != "" {
		where["status"] = req.Status
	}
	if req.Contact != "" {
		where["contact"] = req.Contact
	}
	m := dao.ZmmEmployee.Ctx(ctx)
	total, err = m.Where(where).Count()

	err = m.Where(where).OrderDesc("id").Page(req.PageNum, req.PageSize).FieldsEx("deleted_at").Scan(&list)
	if err != nil {
		return
	}

	return
}

func (s *sZmmEmployee) GetOpQuery(ctx context.Context, req *zmm.ZmmEmployeeOpQueryListReq) (list []*entity.ZmmEmployee, err error) {
	if req.OpQuery == "" {
		return nil, gerror.New("查询参数错误")
	}
	m := dao.ZmmEmployee.Ctx(ctx)
	err = m.Where("status = 1 and  (name like ? or contact like ?)", "%"+req.OpQuery+"%", "%"+req.OpQuery+"%").Fields("id,name").Scan(&list)
	return
}

func (s *sZmmEmployee) Edit(ctx context.Context, req *zmm.ZmmEmployeeEditReq) (id uint64, err error) {
	err = s.hasSameNameAndContact(ctx, req)
	if err != nil {
		return
	}
	err = g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		err = g.Try(ctx, func(ctx context.Context) {
			if req.ID > 0 {
				_, err = dao.ZmmEmployee.Ctx(ctx).TX(tx).Where("id", req.ID).Update(do.ZmmEmployee{
					Name:    req.Name,
					Contact: req.Contact,
					Status:  req.Status,
					Remark:  req.Remark,
				})
			} else {
				insertId, e := dao.ZmmEmployee.Ctx(ctx).TX(tx).InsertAndGetId(do.ZmmEmployee{
					Name:    req.Name,
					Contact: req.Contact,
					Status:  req.Status,
					Remark:  req.Remark,
				})
				req.ID = uint64(insertId)
				err = e
			}

			liberr.ErrIsNil(ctx, err)
		})
		return err
	})
	return req.ID, err
}

func (s *sZmmEmployee) Delete(ctx context.Context, req *zmm.ZmmEmployeeDelReq) error {
	_, err := dao.ZmmEmployee.Ctx(ctx).Where(dao.ZmmEmployee.Columns().Id, req.ID).Delete()
	liberr.ErrIsNil(ctx, err, "删除失败")
	return err
}

func (s *sZmmEmployee) hasSameNameAndContact(ctx context.Context, req *zmm.ZmmEmployeeEditReq) error {
	d := dao.ZmmEmployee.Ctx(ctx).Where("name = ? and contact = ?", req.Name, req.Contact)
	glog.Print(ctx, req.ID)
	if req.ID != 0 {
		d = d.Where("id <> ?", req.ID)
	}
	one, err := d.One()
	if err != nil {
		return err
	}
	if one != nil {
		return gerror.New("已有相同的员工信息（姓名及联系方式一致）")
		//liberr.ErrIsNil(ctx, gerror.New("已有相同的员工信息（姓名及联系方式一致）"), "已有相同的员工信息（姓名及联系方式一致）")
	}
	return nil
}
